package org.boulderfoodrescue.webapp.client.pickups.enterpickupreport;

import java.util.List;

import org.boulderfoodrescue.webapp.client.pickups.selectors.CustomSelector;
import org.boulderfoodrescue.webapp.client.pickups.selectors.LocationSelectorTable;
import org.boulderfoodrescue.webapp.client.pickups.selectors.VolunteerSelectorTable;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.Pickup;
import org.boulderfoodrescue.webapp.shared.PickupReport;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class EnterPickupReportsView extends Composite implements Editor<PickupReport>{

	private static PickupReportViewUiBinder uiBinder = GWT
			.create(PickupReportViewUiBinder.class);

	interface PickupReportViewUiBinder extends
	UiBinder<Widget, EnterPickupReportsView> {
	}

	public EnterPickupReportsView() {
		pickupSelector = new CustomSelector<Pickup>(new PickupSelectorTable(), "Pickup");
		specificPickup = new CustomSelector<SpecificPickup>(new SpecificPickupTable(), "Specific Pickup");
		recipientSelector = new CustomSelector<Location>(new LocationSelectorTable(), "Recipient");
		volunteerSelector = new CustomSelector<UserAccountDetails>(new VolunteerSelectorTable(), "Volunteer");
		volunteerSelector.setEnabled(false);
		initWidget(uiBinder.createAndBindUi(this));
		pickupDate.setFormat(new CustomDateBoxFormat());

		pickupSelector.addHandler(new ValueChangeHandler<Pickup>(){
			@Override
			public void onValueChange(ValueChangeEvent<Pickup> event) {
				disableInputs();
				if(event.getValue() != null)
					enableSpecificInput(event.getValue());
				else
					disableInputs();
			}
		}, ValueChangeEvent.getType());

		specificPickup.addHandler(new ValueChangeHandler<SpecificPickup>(){
			@Override
			public void onValueChange(ValueChangeEvent<SpecificPickup> event) {
				if(event.getValue() != null)
					enableInputs(event.getValue());
			}}, ValueChangeEvent.getType());
		disableInputs();
	}

	@UiField(provided = true)
	CustomSelector<Pickup> pickupSelector;
	@UiField(provided = true)
	CustomSelector<SpecificPickup> specificPickup;
	@UiField(provided = true)
	CustomSelector<Location> recipientSelector;
	@UiField(provided = true)
	CustomSelector<UserAccountDetails> volunteerSelector;
	@UiField
	TextBox weight;
	@UiField
	DateBox pickupDate;
	@UiField
	ListBox foodType;
	@UiField
	TextArea descriptionTextArea;
	@UiField
	TextArea notesTextArea;
	@UiField
	ListBox transportationType;


	private void disableInputs(){
		recipientSelector.setEnabled(false);
		recipientSelector.setValue(null);
		volunteerSelector.setEnabled(false);
		volunteerSelector.setValue(null);
		specificPickup.setEnabled(false);
		specificPickup.setValue(null);
		weight.setEnabled(false);
		pickupDate.setEnabled(false);
		foodType.setEnabled(false);
		descriptionTextArea.setEnabled(false);
		notesTextArea.setEnabled(false);
		transportationType.setEnabled(false);
	}

	private void enableInputs(SpecificPickup sPickup){
		Pickup pickup = pickupSelector.getValue();
		recipientSelector.setValue(pickup.getRecipient());
		recipientSelector.setEnabled(true);
		volunteerSelector.setValue(pickup.getVolunteer());
		volunteerSelector.setEnabled(true);
		weight.setEnabled(true);
		pickupDate.setValue(sPickup.getDate());
		pickupDate.setEnabled(true);
		foodType.setSelectedIndex(pickup.getDonor().getFoodTypeAsInt());
		foodType.setEnabled(true);
		descriptionTextArea.setEnabled(true);
		notesTextArea.setEnabled(true);
		transportationType.setSelectedIndex(pickup.getTransportationAsLong().intValue());
		transportationType.setEnabled(true);
	}

	private void enableSpecificInput(Pickup pickup){
		List<SpecificPickup> sPickups = SpecificPickupFactory.getSpecificPickups(pickup);
		for(SpecificPickup sp : sPickups){
			specificPickup.addEntry(sp);
		}
		specificPickup.setEnabled(true);
	}

	public void addPickup(Pickup p) {
		pickupSelector.addEntry(p);
	}
}
